﻿using System;
using System.Diagnostics.Tracing;
using System.Threading;

namespace Sustainalytics.Governance.Service
{
    [EventSource(Name = "Sustainalytics-Governance-Service")]
    public sealed class GovernanceEventSource : EventSource
    {
        private static readonly Lazy<GovernanceEventSource> _lazy = new Lazy<GovernanceEventSource>(InitLog, LazyThreadSafetyMode.ExecutionAndPublication);

        public static GovernanceEventSource Log => _lazy.Value;

        [NonEvent]
        private static GovernanceEventSource InitLog()
        {
            var log = new GovernanceEventSource();
            log.Ping();
            return log;
        }

        [Event(1, Level = EventLevel.LogAlways)]
        private void Ping()
        {
            if (!IsEnabled()) return;
            WriteEvent(1);
        }

        [NonEvent]
        public void LogError(Exception ex)
        {
            LogError(ex.ToString());
        }

        [Event(3, Level = EventLevel.Error)]
        private void LogError(string msg)
        {
            if (!IsEnabled()) return;
            WriteEvent(3, msg);
        }
    }
}
